#include "atomic_0813_dispatch_allocation_service.h"


// 模拟节点资源信息结构体
struct NodeResource {
    int node_id;               // 节点ID
    std::string node_ip;       // 节点IP
    int available_cpu_cores;   // 可用CPU核心数
    int available_memory_mb;   // 可用内存(MB)
    int available_gpu_count;   // 可用GPU数量
};

// 模拟节点资源列表（实际应用中可能从配置文件或数据库加载）
static std::vector<NodeResource> mock_nodes = {
    {1, "192.168.1.101", 8, 16384, 2},   // 节点1：8核CPU，16GB内存，2块GPU
    {2, "192.168.1.102", 4, 8192, 1},    // 节点2：4核CPU，8GB内存，1块GPU
    {3, "192.168.1.103", 16, 32768, 4},  // 节点3：16核CPU，32GB内存，4块GPU
    {4, "192.168.1.104", 2, 4096, 0}     // 节点4：2核CPU，4GB内存，无GPU
};

int getSchedulerAllocation(SchedulerInput *schedulerInput, SchedulerOutput *schedulerOutput) {
    // 1. 检查输入参数有效性
    if (schedulerInput == nullptr || schedulerOutput == nullptr) {
        return ATOMIC_SERVICE_ERROR; // 无效指针
    }

    // 2. 检查任务资源需求合法性
    if (schedulerInput->cpu_cores < 0 || schedulerInput->memory_mb < 0 || schedulerInput->gpu_count < 0) {
        // 设置输出错误信息
        schedulerOutput->return_type = ATOMIC_SERVICE_ERROR;
        schedulerOutput->status = "Invalid resource requirement";
        schedulerOutput->log_level = 3; // ERROR级别
        schedulerOutput->content = "CPU cores, memory or GPU count cannot be negative";
        schedulerOutput->module_name = "DispatchAllocation";
        return ATOMIC_SERVICE_ERROR;
    }

    // 3. 初始化输出结构体公共字段
    schedulerOutput->service_id = 813; // 服务ID，与服务名称中的0813对应
    schedulerOutput->service_name = "atomic_0813_dispatch_allocation_service";
    schedulerOutput->task_id = schedulerInput->task_id;
    schedulerOutput->module_name = "DispatchAllocation";

    // 获取当前时间戳（秒级）
    auto now = std::chrono::system_clock::now();
    auto timestamp = std::chrono::duration_cast<std::chrono::seconds>(
        now.time_since_epoch()
    ).count();
    schedulerOutput->timestamp = static_cast<double>(timestamp);

    // 4. 执行调度逻辑：寻找满足资源需求的节点
    bool allocated = false;
    for (const auto& node : mock_nodes) {
        // 检查节点资源是否满足任务需求
        if (node.available_cpu_cores >= schedulerInput->cpu_cores &&
            node.available_memory_mb >= schedulerInput->memory_mb &&
            node.available_gpu_count >= schedulerInput->gpu_count) {
            
            // 找到合适节点，设置分配结果
            schedulerOutput->assigned_node_id = node.node_id;
            schedulerOutput->node_ip = node.node_ip;
            schedulerOutput->return_type = ATOMIC_SERVICE_SUCCESS;
            schedulerOutput->status = "Allocated";
            schedulerOutput->log_level = 1; // INFO级别
            schedulerOutput->content = "Task " + std::to_string(schedulerInput->task_id) + 
                                      " allocated to node " + std::to_string(node.node_id);
            allocated = true;
            break;
        }
    }

    // 5. 处理未找到合适节点的情况
    if (!allocated) {
        schedulerOutput->assigned_node_id = -1;
        schedulerOutput->node_ip = "";
        schedulerOutput->return_type = ATOMIC_SERVICE_FAIL;
        schedulerOutput->status = "Allocation failed";
        schedulerOutput->log_level = 3; // ERROR级别
        schedulerOutput->content = "No available node meets resource requirements for task " + 
                                  std::to_string(schedulerInput->task_id);
        return ATOMIC_SERVICE_FAIL;
    }

    return ATOMIC_SERVICE_SUCCESS;
}